package org.abc.fund.controller.dataUpdate;
import lombok.extern.slf4j.Slf4j;
import org.abc.fund.service.dataSourceCollection.FundAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;

@RestController
@RequestMapping("/api/announcement/update")
@Slf4j
public class AnnouncementUpdateController {
    
    @Autowired
    private FundAnnouncementService announcementService;

    /**
     * 手动触发全量更新（原有功能）
     */
    @PostMapping("/trigger/full")
    public ResponseEntity<String> triggerFullUpdate() {
        log.info("手动触发全量公告更新");
        announcementService.fetchAllAnnouncements();
        return ResponseEntity.ok("全量公告更新完成");
    }

    /**
     * 手动触发季度更新
     */
    @PostMapping("/trigger/quarterly")
    public ResponseEntity<String> triggerQuarterlyUpdate() {
        log.info("手动触发季度公告更新");
        announcementService.fetchQuarterlyAnnouncements();
        return ResponseEntity.ok("季度公告更新完成");
    }

    /**
     * 手动触发半年度更新
     */
    @PostMapping("/trigger/semi-annual")
    public ResponseEntity<String> triggerSemiAnnualUpdate() {
        log.info("手动触发半年度公告更新");
        announcementService.fetchSemiAnnualAnnouncements();
        return ResponseEntity.ok("半年度公告更新完成");
    }

    /**
     * 手动触发重大事件更新
     */
    @PostMapping("/trigger/important-events")
    public ResponseEntity<String> triggerImportantEventsUpdate() {
        log.info("手动触发重大事件更新");
        announcementService.fetchImportantEventsAnnouncements();
        return ResponseEntity.ok("重大事件更新完成");
    }

    /**
     * 手动触发数据清理
     */
    @PostMapping("/trigger/cleanup")
    public ResponseEntity<String> triggerCleanup() {
        log.info("手动触发数据清理");
        announcementService.cleanupOldAnnouncements();
        return ResponseEntity.ok("数据清理完成");
    }

    /**
     * 手动触发指定时间范围的更新
     */
    @PostMapping("/trigger/since")
    public ResponseEntity<String> triggerUpdateSince(@RequestParam String sinceDate) {
        log.info("手动触发指定时间范围更新：{}", sinceDate);
        try {
            LocalDate date = LocalDate.parse(sinceDate);
            announcementService.fetchAnnouncementsSince(date, "手动更新");
            return ResponseEntity.ok("指定时间范围更新完成");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("日期格式错误，请使用 yyyy-MM-dd 格式");
        }
    }
}